import { asyncRoutes } from '@/router'
import { matchAsyncRoutes, getNavList } from '@/router/permission'

const state = {
  routes: [],
  accessedRoutes: [],
  navList: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.accessedRoutes = routes
  },

  SET_NAV_LIST: (state, list) => {
    state.navList = list
  }
}

const actions = {
  generateRoutes({ rootState, commit }) {
    return new Promise(resolve => {
      const menu = rootState.user.menuTree
      console.log('vuex permission menu', menu)
      commit('SET_ROUTES', matchAsyncRoutes(menu, asyncRoutes))
    })
  },

  generateNavList({ commit }) {
    return new Promise(resolve => {
      commit('SET_NAV_LIST', getNavList())
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
